package com.yonyou.marketing.app.api.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.marketing.common.Response;
import org.marketing.common.dto.maintenanceorder.MaintenanceDto;
import org.marketing.common.util.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.yonyou.marketing.app.api.entity.EvaluationResultDetailPO;
import com.yonyou.marketing.app.api.entity.EvaluationResultPO;

@Service("appRepairRecordService")
public class AppRepairRecordService {
	
	private Logger          logger = Logger.getLogger(AppQuestionnaireService.class);
	
	@Autowired
    private RepairRecordInterface    repairRecordInterface;
	@Autowired
    private TicketServiceInterface    ticketServiceInterface;
	
	@SuppressWarnings("unchecked")
	public Map<String, Object> maintenanceorder(String userId,String vin){
		logger.info("调用维修履历服务  SERVICE-MAINTENANCEORDER==>start...");
		Map<String, Object> result = new HashMap<>();
		Map<String, Object> resultMap = new HashMap<>();
		List<Map<String, Object>> mainList = new ArrayList<>();
		List<Map<String, Object>> carInfoList = new ArrayList<>();
		result = repairRecordInterface.maintenanceorder(userId,vin);
		//把数据转成app需要的格式
		if(!CheckUtil.NullOrEmpty(result)&&!result.isEmpty()){
			List<Map<String, Object>> maList =  (List<Map<String, Object>>) result.get("maintenenceOrder");
			for (int i = 0; i < maList.size(); i++) {
				Map<String, Object> mDto = maList.get(i);
				Map<String, Object> map = new HashMap<>();//ifEvaluation
				map.put("maintenanceWorkOrderNum", mDto.get("maintenanceWorkOrderNum")==null?"":mDto.get("maintenanceWorkOrderNum"));
				map.put("maintenanceWorkOrderType", mDto.get("maintenanceWorkOrderType")==null?"":mDto.get("maintenanceWorkOrderType"));
				map.put("createDate", mDto.get("createDate")==null?"":mDto.get("createDate"));
				map.put("repairStatus", mDto.get("repairStatus")==null?"":mDto.get("repairStatus"));
				map.put("actualCompleteDate", mDto.get("actualCompleteDate")==null?"":mDto.get("actualCompleteDate"));
				map.put("dealerName", mDto.get("dealerName")==null?"":mDto.get("dealerName"));
				map.put("dealerCode", mDto.get("dealerCode")==null?"":mDto.get("dealerCode"));
				map.put("ifEvaluation", mDto.get("ifEvaluation")==null?"":mDto.get("ifEvaluation"));
				mainList.add(map);
			}
			resultMap.put("maintenenceOrder", mainList);
			
			List<Map<String, Object>> carList = (List<Map<String, Object>>) result.get("carInfo");
			for (int i = 0; i < carList.size(); i++) {
				Map<String, Object> carDto = carList.get(i);
				Map<String, Object> map = new HashMap<>();
				map.put("carId", carDto.get("carId")==null?"":carDto.get("carId"));
				map.put("vin", carDto.get("vin")==null?"":carDto.get("vin"));
				map.put("engine", carDto.get("engine")==null?"":carDto.get("engine"));
				map.put("modelCodeNew", carDto.get("modelCodeNew")==null?"":carDto.get("modelCodeNew"));
				map.put("buyerTel", carDto.get("buyerTel")==null?"":carDto.get("buyerTel"));
				map.put("billDate", carDto.get("billDate")==null?"":carDto.get("billDate"));
				map.put("province", carDto.get("province")==null?"":carDto.get("province"));
				map.put("city", carDto.get("city")==null?"":carDto.get("city"));
				map.put("linkMan", carDto.get("linkMan")==null?"":carDto.get("linkMan"));
				map.put("sex", carDto.get("sex")==null?"":carDto.get("sex"));
				map.put("phone", carDto.get("phone")==null?"":carDto.get("phone"));
				map.put("modelName", carDto.get("modelName")==null?"":carDto.get("modelName"));
				map.put("seriesCode", carDto.get("seriesCode")==null?"":carDto.get("seriesCode"));
				map.put("licenseNo", carDto.get("licenseNo")==null?"":carDto.get("licenseNo"));
				carInfoList.add(map);
			}
			resultMap.put("carInfo", carInfoList);
		}
		return resultMap;
	}
	
	public Map<String, Object> maintenanceorderDetail(String  orderNum,String dealerCode){
		logger.info("调用维修履历服务  SERVICE-MAINTENANCEORDER==>start...");
		List<MaintenanceDto> list = repairRecordInterface.detail(orderNum,dealerCode);
		Map<String, Object> result = new HashMap<>();
		if(!CheckUtil.NullOrEmpty(list)&&!list.isEmpty()){
			result.put("maintenanceWorkOrderNum", list.get(0).getMaintenanceWorkOrderNum()==null?"":list.get(0).getMaintenanceWorkOrderNum());
			result.put("inMile", list.get(0).getInMile()==null?"":list.get(0).getInMile());
			result.put("actualCompleteDate", list.get(0).getActualCompleteDate()==null?"":list.get(0).getActualCompleteDate());
			result.put("expectedTime", list.get(0).getExpectedTime()==null?"":list.get(0).getExpectedTime());
			result.put("deliveryTime", list.get(0).getDeliveryTime()==null?"":list.get(0).getDeliveryTime());
			result.put("dealerName", list.get(0).getDealerName()==null?"":list.get(0).getDealerName());
			result.put("dealerCode", list.get(0).getDealerCode()==null?"":list.get(0).getDealerCode());
			result.put("saName", list.get(0).getSaName()==null?"":list.get(0).getSaName());
			result.put("completionDate", list.get(0).getCompletionDate()==null?"":list.get(0).getCompletionDate());
			result.put("repairStatus", list.get(0).getRepairStatus()==null?"":list.get(0).getRepairStatus());
			result.put("projectInfo", list.get(0).getProjectInfo()==null?"":list.get(0).getProjectInfo());
			result.put("partInfo", list.get(0).getPartInfo()==null?"":list.get(0).getPartInfo());
			result.put("remark", list.get(0).getRemark()==null?"":list.get(0).getRemark());
			result.put("maintenanceWorkOrderType", list.get(0).getMaintenanceWorkOrderType()==null?"":list.get(0).getMaintenanceWorkOrderType());
		}
		return result;
	}
	public EvaluationResultPO addEvaluation(Integer type,String orderNumber,Integer evaType,String userId){
		logger.info("调用维修履历服务  SERVICE-MAINTENANCEORDER==>start...");
		Response<EvaluationResultPO> list = repairRecordInterface.addEvaluation(type, orderNumber, evaType, userId);
		
		return list.getData();
	}
	public Map<String, Object> checkEvaluation(Integer type,String orderNumber,String userId){
		logger.info("调用维修履历服务  SERVICE-MAINTENANCEORDER==>start...");
		Response<Map<String, Object>> list = repairRecordInterface.checkEvaluation(type, orderNumber, userId);
		
		return list.getData();
	}
	
	public Map<String, Object> saveEvaluation(Map<String,Object> param){
		String userId = param.get("userId").toString();
    	String evaluationContent = param.get("evaluationContent").toString();
    	String orderNumber = param.get("orderNumber").toString();
    	String dealerCode = param.get("dealerCode").toString();
    	Integer evaluationId = Integer.valueOf(param.get("evaluationId").toString());
    	String evaluationDetail = param.get("evaluationDetail").toString();
    	List<EvaluationResultDetailPO> listEva = new ArrayList<>();
    	for (String detail : evaluationDetail.split("#")) {
    		EvaluationResultDetailPO dpo = new EvaluationResultDetailPO();
    		dpo.setDetailId(Integer.valueOf(detail.substring(0, detail.indexOf("@"))));
    		dpo.setStarSum(Integer.valueOf(detail.substring(detail.indexOf("@")+1)));
    		listEva.add(dpo);
		}
    	EvaluationResultPO po = new EvaluationResultPO();
    	po.setEvaluationContent(evaluationContent);
    	po.setOrderNumber(orderNumber+dealerCode);
    	po.setEvaluationId(evaluationId);
    	po.setEvaluationResultDetailPO(listEva);
    	
		logger.info("调用维修履历服务  SERVICE-MAINTENANCEORDER==>start...");
		Response<Map<String, Object>> list = repairRecordInterface.saveEvaluation(po, userId);
		
		return list.getData();
	}
	public PageInfo<Map<String, Object>> getTicketList(Map<String,Integer> map){
		Integer userId = map.get("userId");
		logger.info("调用优惠券服务  SERVICE-POINTS==>start...");
		Response<PageInfo<Map<String, Object>>> list = ticketServiceInterface.ticketList(map.get("pageNum")
				, map.get("pageSize"), map.get("status"), userId.toString());
		
		return list.getData();
	}
}
